package cn.itsource.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.itsource.domain.User;
import cn.itsource.exception.BusinessException;
import cn.itsource.mapper.UserMapper;
import cn.itsource.query.UserQuery;
import cn.itsource.service.UserService;
import cn.itsource.util.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 业务实现类：用户User
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public User findOne(Long id) {
        return userMapper.findOne(id);
    }

    @Transactional
    @Override
    public void add(User user) { //
        //1.先生成随机的盐值
        String salt = RandomUtil.randomString(32);
        //md5是不可逆：只能加密，不能解密  //对原始密码加密：先加密 + 再加盐
        String md5Pwd = SecureUtil.md5(user.getPassword() + salt);

        user.setSalt(salt);
        user.setPassword(md5Pwd);//加密加盐之后的密码
        userMapper.add(user);
    }

    @Transactional
    @Override
    public void update(User user) {
        //修改的时候密码和盐值不修改，单独修改密码的时候再修改
        userMapper.update(user);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }

    @Override
    public PageInfo<User> pageQuery(UserQuery query) {
       /* //查询总条数 - 原生分页的写法
        Integer totals = userMapper.queryCount();
        //查询当前页的分页数据
        List<User> list = userMapper.queryData(query);*/
        //开启分页：1.首先查询总数量 2.根据驱动名使用相应的方式进行分页查询，如果是mysql，就会用limit 3.自动计算limit的第1个参数
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<User> list = userMapper.pageQuery(query);
        return new PageInfo<>(list);
    }

    @Override
    public void patchDel(Long[] ids) {
        userMapper.patchDel(ids);
    }

    @Override
    public User login(Map<String, String> map) {
        String account = map.get("account");//和前端一致
        String checkPass = map.get("checkPass");//和前端一致
        //一：校验 - 空值【前端也校验了(不安全，容易被跳过)，但后端一样的校验】
        if(StrUtil.isBlank(account) || StrUtil.isBlank(checkPass)){
            throw new BusinessException("信息不能为空");
        }

        //二：校验用户名
        User user = userMapper.findByAccount(account);
        if(user==null){//根据账号没有查询到信息
            throw new BusinessException("账号不存在");
        }

        //三：校验密码
        String md5Pwd = SecureUtil.md5(checkPass + user.getSalt());
        if(!md5Pwd.equals(user.getPassword())){
            throw new BusinessException("密码错误");
        }

        return user;
    }
}
